package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import beans.IngredienteBean;
import beans.InsumoBean;
import beans.ProductoBean;
import beans.SemielaboradoBean;
import beans.SimpleBean;

public class HibernateProductoDAO {
	private static HibernateProductoDAO instancia;
	private static SessionFactory sf;

	private HibernateProductoDAO() {

	}

	public static HibernateProductoDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateProductoDAO();
		}
		return instancia;
	}

	public void grabarProducto(ProductoBean producto) {
		Session sesion = sf.openSession();
		sesion.beginTransaction();
		sesion.persist(producto);
		sesion.flush();
		sesion.getTransaction().commit();
		sesion.close();

	}

	public IngredienteBean buscarIngrediente(IngredienteBean i) {
		Session session = sf.openSession();
		IngredienteBean ingrediente = (IngredienteBean) session.get(
				IngredienteBean.class, i.getId());
		session.close();
		return ingrediente;
	}

	public SimpleBean buscarSimple(SimpleBean s) {
		Session session = sf.openSession();
		SimpleBean simple = (SimpleBean) session.get(SimpleBean.class,
				s.getId());
		session.close();
		return simple;
	}

	public SemielaboradoBean buscarSemielaborado(SemielaboradoBean s) {
		Session session = sf.openSession();
		SemielaboradoBean semielaboradoBean = (SemielaboradoBean) session.get(
				SemielaboradoBean.class, s.getId());
		session.close();
		return semielaboradoBean;
	}

	public InsumoBean buscarInsumo(InsumoBean i) {
		Session session = sf.openSession();
		InsumoBean insumoBean = (InsumoBean) session.get(InsumoBean.class,
				i.getId());
		session.close();
		return insumoBean;
	}

	public void update(ProductoBean producto3) {
		Session sesion = sf.openSession();
		sesion.beginTransaction();
		sesion.save(producto3);
		sesion.flush();
		sesion.getTransaction().commit();
		sesion.close();

	}

	public List<IngredienteBean> listarIngredientes() {
		Session session = sf.openSession();
		List<IngredienteBean> ls = (List<IngredienteBean>) session.createQuery(
				"from IngredienteBean").list();
		session.close();
		return ls;
	}

	public List<SimpleBean> listarSimples() {
		Session session = sf.openSession();
		List<SimpleBean> ls = (List<SimpleBean>) session.createQuery(
				"from SimpleBean").list();
		session.close();
		return ls;
	}

	public List<InsumoBean> listarInsumos() {
		Session session = sf.openSession();
		List<InsumoBean> ls = (List<InsumoBean>) session.createQuery(
				"from InsumoBean").list();
		session.close();
		return ls;
	}

	public List<SemielaboradoBean> listarSemielaborados() {
		Session session = sf.openSession();
		List<SemielaboradoBean> ls = (List<SemielaboradoBean>) session
				.createQuery("from SemielaboradoBean").list();
		session.close();
		return ls;
	}
}
